<?php

    if(!defined('BASEPATH'))
        exit('No direct script access allowed');

    /**
     * Main class for working with site pages named "posts".
     * @author NU SIT Bar
     * @version 1.0
     * @created 17.Jul.2012 17:39:44 h
     */
    class Pages extends MX_Controller
    {

        private $data;

        function __construct()
        {

            parent::__construct();
            $this->load->model('pages_model');
            $this->load->library('translate', array('lang' => $this->session->userdata('my_language')));
            $this->load->config('pages');

        }

        public function index($params)
        {
            //print_r($params['link']);
            $this->data['link']=$params['link'];
            if(!isset($_GET['post_type']))
            {
                $_GET['post_type']="page";
            }
            $this->view_all();

        }

        /**
         * 
         * @param $_GET['post_type']
         */
        function view_all()
        {

            if(!isset($_GET['post_type']))
            {
                show_error('Please set post_type!');
            }

            $this->data['page']=ucfirst($_GET['post_type']);
            $this->data['title']="View All ".$_GET['post_type'];
            $this->data['post_type']=$_GET['post_type'];
            $this->data['all_posts']=$this->translate->trans_res_array($this->pages_model->view_all($_GET['post_type']));
            echo $this->load->view('view_all', $this->data, TRUE);
            //echo $this->template->build('view_all', $this->data);

        }

        function view_new($params)
        {
            $this->data['link']=$params['link'];
            $this->data['page']=ucfirst($_GET['post_type']);
            $this->data['title']="Add ".$_GET['post_type'];
            $this->data['post_type']=$_GET['post_type'];
            $this->data['post_action']="NEW";
            $this->data['all_posts']=$this->translate->trans_res_array($this->pages_model->view_all($_GET['post_type']));
            echo $this->load->view('edit_page', $this->data, TRUE);
            //$this->template->build('edit_page', $this->data);

        }

        /**
         * 
         * @param $_GET['post_ID']
         * @param $_GET['post_type']
         */
        function view_edit($params)
        {
            $this->data['link']=$params['link'];
            $this->data['page']=ucfirst($_GET['post_type']);
            $this->data['title']="Edit ".ucfirst($_GET['post_type']);
            $this->data['post_type']=$_GET['post_type'];
            $this->data['post_action']="EDIT";
            $this->data['post']=$this->pages_model->view_edit($_GET['post_ID']);
            $this->data['all_posts']=$this->translate->trans_res_array($this->pages_model->view_all($_GET['post_type']));
            echo $this->load->view('edit_page', $this->data, TRUE);
            //$this->template->build('edit_page',$this->data);

        }

        /**
         * 
         * @param $_POST['parent_id']
         * @param $_POST['post_type']
         * @param $_POST['post_title']
         * @param $_POST['post_action']
         * @param $_POST['content']
         * @param $_POST['qtrans_textarea_content']
         */
        function submit_post()
        {

            $_POST['post_title']=$this->translate->regular_array_to_col($_POST['title']);
            $_POST['content']=$this->translate->regular_array_to_col($_POST['content']);


            // Clear html special character from title if exist
            $_POST['post_title']=str_replace('"', '', $_POST['post_title']);

            // Set translate language for column Name
            $this->translate->setLang('me');

            switch($_POST['post_action'])
            {

                case 'NEW':

                    $result=$this->pages_model->create_page(
                            $_POST['post_title'], $this->translate->getArray($_POST['post_title'], TRUE), $_POST['content'], $_POST['parent_id'], $_POST['post_type']
                    );

                    $message='Successful insert operation';

                    break;

                case 'EDIT':

                    $result=$this->pages_model->update_page(
                            $_POST['post_ID'], $_POST['post_title'], $this->translate->getArray($_POST['post_title'], TRUE), $_POST['content'], $_POST['parent_id'], $_POST['post_type']
                    );

                    $message='Successful edit operation';

                    break;
            }

            return $result.'-'.$message;
            // Return
            /* if($result){
              echo json_encode(array('action'=>true,'msg'=>$message));
              }else{
              echo json_encode(array('action'=>true,'msg'=>'Something went wrong. Please try again.'));
              } */

        }

        function delete($params)
        {

            if($this->pages_model->delete_page($params['ID']))
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }

        }

    }

    /* End of file pages.php */
    /* Location: ./application/modules/pages/controllers/pages.php */